﻿using BLL;
using DBUtility;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UtilityHelper;

namespace IYAKASAPortal.Login
{
    public partial class LoginCamera : System.Web.UI.Page
    {
        private CameraHelper cameraHelper { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

            int line = GetLineNum();
            if (!IsPostBack)
            {
                #region 获取用户名和密码
                CAMERA_INFO_BLL bll = new CAMERA_INFO_BLL();
                CAMERA_INFO info = bll.GetModel(1);
                txtUserName.Text = info.UserName;
                txtPwd.Attributes.Add("value", info.UserPwd);

                CONST_DEFINE_WEB_BLL constBll = new CONST_DEFINE_WEB_BLL();
                CONST_DEFINE_WEB model = constBll.GetModel(39);
                txtServerIP.Text = model.ValueOfName;
                txtServerPort.Text = model.Code;

                #endregion


                List<Button> btnList = new List<Button>();
                btnList.Add(btnLineNum1);
                btnList.Add(btnLineNum2);
                btnList.Add(btnLineNum3);
                btnList.Add(btnLineNum4);
                foreach (Button btn in btnList)
                {
                    int i = Convert.ToInt32(btn.Text);
                    if (i == line)
                    {
                        btn.BackColor = Color.Green;
                    }
                }


                GetCameraInfo();
            }

            cameraHelper = CameraHelper.GetInstance();


            if (cameraHelper.InitSDK)
            {
                labWarnStr.Text = "SDK初始化成功!";
                btnInit.Enabled = false;
            }

            if (cameraHelper.GetIsAlert(line))
            {
                btnSetAlarm.Text = "撤销布防";
            }
            else
            {
                btnSetAlarm.Text = "设置布防";
            }

        }

        /// <summary>
        /// 初始化SDK
        /// </summary>
        private void InitSDK()
        {
            try
            {
                (CameraHelper.GetInstance()).InitSDK = CHCNetSDK.NET_DVR_Init();
                if ((CameraHelper.GetInstance()).InitSDK)
                {

                    btnInit.Enabled = false;
                    labWarnStr.Text = "SDK初始化成功!";
                }
                else
                {
                    labWarnStr.Text = "SDK初始化失败!";
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 更新表中摄像头用户名和密码
        /// </summary>
        private void InitCameraUser()
        {
            try
            {
                if ((CameraHelper.GetInstance()).InitSDK)
                {
                    CAMERA_INFO_BLL bll = new CAMERA_INFO_BLL();
                    CAMERA_INFO info = new CAMERA_INFO();
                    info.UserName = txtUserName.Text;
                    info.UserPwd = txtPwd.Text;
                    info.CameraIP = txtIPAdress.Text;
                    info.CameraPort = txtPort.Text;

                    CONST_DEFINE_WEB_BLL constBll = new CONST_DEFINE_WEB_BLL();
                    CONST_DEFINE_WEB model = new CONST_DEFINE_WEB();
                    model.ValueOfName = txtServerIP.Text;
                    model.Code = txtServerPort.Text;
                    constBll.Update(model, 39);   //???ID号

                    txtPwd.Attributes.Add("value", txtPwd.Text);



                    SqlParameter[] param = new SqlParameter[]{
                        new SqlParameter("@UserName",info.UserName),
                         new SqlParameter("@UserPwd",info.UserPwd),
                           new SqlParameter("@CameraIp",info.CameraIP),
                             new SqlParameter("@CameraPort",info.CameraPort),
                               new SqlParameter("@LineNum",GetLineNum()),
                     };

                    DbStoredProcedure dbHelper = new DbStoredProcedure();
                    dbHelper.RunProc("proc_CAMERA_INFO_UpdateCameraInfo", param);

                    labWarnStr.Text = "保存成功!";

                    /*if (bll.Update(info, m_lineNum)) //???ID号
                    {
                        labWarnStr.Text = "保存成功!";
                    }*/
                }
                else
                {
                    labWarnStr.Text = "SDK未初始化!";
                }
            }
            catch (Exception ex)
            {

            }

        }

        private int GetLineNum()
        {
            int line = 1;
            //获取安检线号，获取失败则默认为1号线
            if (Request["LineNum"] == null || !int.TryParse(Request["LineNum"].ToString(), out line))
            {
                line = 1;
            }

            return line;
        }

        /// <summary>
        /// 验证、登录摄像头
        /// </summary>
        private void ValidateCamera()
        {
            try
            {
                cameraHelper = CameraHelper.GetInstance();
                if (cameraHelper.InitSDK)
                {
                    cameraHelper.RegisterCamera(GetLineNum());
                    if (cameraHelper.GetIsRegister(GetLineNum()))
                    {
                        labWarnStr.Text = "摄像头注册成功!";
                    }
                    else
                    {
                        labWarnStr.Text = "摄像头注册失败!";
                    }
                }
                else
                {
                    labWarnStr.Text = "SDK未初始化!";
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 获取摄像头IP、端口等信息
        /// </summary>
        private void GetCameraInfo()
        {
            try
            {
                CAMERA_INFO_BLL bll = new CAMERA_INFO_BLL();
                var Infos = bll.GetModelList("IsEnabled='True' and LineNum='" + GetLineNum().ToString() + "'");
                if (Infos.Length > 0)
                {
                    txtPort.Text = Infos[0].CameraPort;
                    txtIPAdress.Text = Infos[0].CameraIP;
                }
                else
                {
                    txtPort.Text = string.Empty;
                    txtIPAdress.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 设置布防
        /// </summary>
        private bool SetAlarm()
        {

            (CameraHelper.GetInstance()).SetAlert(GetLineNum());
            return (CameraHelper.GetInstance()).GetIsAlert(GetLineNum());

        }

        /// <summary>
        /// 撤销布防
        /// </summary>
        private bool CloseAlarm()
        {
            (CameraHelper.GetInstance()).CloseAlert(GetLineNum());
            return !(CameraHelper.GetInstance()).GetIsAlert(GetLineNum());

        }


        #region Button事件
        /// <summary>
        /// 验证摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnValidate_Click(object sender, EventArgs e)
        {
            try
            {
                ValidateCamera();
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                InitCameraUser();
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 初始化SDK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInit_Click(object sender, EventArgs e)
        {
            try
            {
                InitSDK();
            }
            catch (Exception ex)
            {

            }
        }


        /// <summary>
        /// 设置布防
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSetAlarm_Click(object sender, EventArgs e)
        {
            try
            {
                cameraHelper = CameraHelper.GetInstance();
                if (btnSetAlarm.Text == "撤销布防")
                {
                    if (CloseAlarm())
                    {
                        labWarnStr.Text = "撤销布防成功!";
                        btnSetAlarm.Text = "设置布防";
                    }
                }
                else
                {
                    if (cameraHelper.GetIsRegister(GetLineNum()))
                    {
                        if (SetAlarm())
                        {
                            labWarnStr.Text = "布防成功!";
                            btnSetAlarm.Text = "撤销布防";
                        }
                        else
                        {
                            labWarnStr.Text = "布防失败!";
                        }
                    }
                    else
                    {
                        labWarnStr.Text = "摄像头未注册!";
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            try
            {
                if ((CameraHelper.GetInstance()).CleanUp())
                {
                    labWarnStr.Text = "释放资源成功！";
                    btnInit.Enabled = true;
                }
                else
                {
                    labWarnStr.Text = "释放资源失败!";
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 更改线路
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLineNum1_Click(object sender, EventArgs e)
        {
            try
            {
                Button btn = (Button)sender;

                int line = Convert.ToInt32(btn.Text);

                gfAjaxJs.SetGoToUrl(this.UpdatePanel1, string.Format("LoginCamera.aspx?LineNum={0}", line));
            }
            catch (Exception ex)
            {

            }

        }
        #endregion


    }
}